import requestSecurity from "/src/components/node-kantboot/libs/requestSecurity";
import storage from "/src/components/node-kantboot/libs/storage";

/**
 * 获取所有工作系列
 */
let requestWorkSeriesList = function() {
    return new Promise((resolve, reject) => {
        requestSecurity.send({
            uri: "/business-talent-web/series/getAll",
            stateSuccess: (res) => {
                console.debug("获取工作系列列表成功", res.data);
                // 保存工作系列列表到缓存
                storage.set("workSeriesList", res.data);
                // start:保存工作系列map到缓存
                let workSeriesMap = {};
                res.data.forEach((item) => {
                    workSeriesMap["workSeries_" + item.id] = item;
                });
                storage.set("workSeriesMap", workSeriesMap);
                // end:保存工作系列map到缓存
                resolve();
            },
            stateFail: (res) => {
                uni.showToast({
                    title: "读取工作系列列表错误：" + res.errMsg,
                    icon: "none"
                })
                reject();
            }
        })
    });
}

/**
 * 获取技能等级计划列表
 */
let requestSkillLevelList = function() {
    return new Promise((resolve, reject) => {
        requestSecurity.send({
            uri: "/business-talent-web/skillLevel/getAll",
            stateSuccess: (res) => {
                console.debug("获取技能等级计划列表成功", res.data);
                // 保存技能等级计划列表到缓存
                storage.set("skillLevelList", res.data);
                // start:保存技能等级计划map到缓存
                let skillLevelMap = {};
                res.data.forEach((item) => {
                    skillLevelMap["skillLevel_" + item.id] = item;
                });
                storage.set("skillLevelMap", skillLevelMap);
                // end:保存技能等级计划map到缓存
                resolve();
            },
            stateFail: (res) => {
                uni.showToast({
                    title: "读取技能等级计划列表错误：" + res.errMsg,
                    icon: "none"
                })
                reject();
            }
        })
    });
}

/**
 * 获取人才计划列表
 */
let requestTalentPlanList = function() {
    return new Promise((resolve, reject) => {
        requestSecurity.send({
            uri: "/business-talent-web/plan/getAll",
            stateSuccess: (res) => {
                console.debug("获取人才计划列表成功", res.data);
                // 保存人才计划列表到缓存
                storage.set("talentPlanList", res.data);
                // start:保存人才计划map到缓存
                let talentPlanMap = {};
                res.data.forEach((item) => {
                    talentPlanMap["talentPlan_"+item.id] = item;
                });
                storage.set("talentPlanMap", talentPlanMap);
                // end:保存人才计划map到缓存
                resolve();
            },
            stateFail: (res) => {
                uni.showToast({
                    title: "读取人才计划列表错误："+res.errMsg,
                    icon: "none"
                })
                reject();
            }
        })
    });
}


/**
 * 获取人才奖项的列表
 */
let requestTalentAwardList = function() {
    return new Promise((resolve, reject) => {
        requestSecurity.send({
            uri: "/business-talent-web/award/getAll",
            stateSuccess: (res) => {
                console.debug("获取人才奖项列表成功", res.data);
                // 保存人才奖项列表到缓存
                storage.set("talentAwardList", res.data);
                // start:保存人才奖项map到缓存
                let talentAwardMap = {};
                res.data.forEach((item) => {
                    talentAwardMap["talentAward_" + item.id] = item;
                });
                storage.set("talentAwardMap", talentAwardMap);
                // end:保存人才奖项map到缓存
                resolve();
            },
            stateFail: (res) => {
                uni.showToast({
                    title: "读取人才奖项列表错误：" + res.errMsg,
                    icon: "none"
                })
                reject();
            }
        })
    });
}

/**
 * 获取所有学科专业
 * /tool-edu/major/getAll
 */
let requestEduMajorList = function() {
    return new Promise((resolve, reject) => {
        requestSecurity.send({
            uri: "/tool-edu/major/getAll",
            stateSuccess: (res) => {
                console.debug("获取学科专业列表成功", res.data);
                // 保存学科专业列表到缓存
                storage.set("eduMajorList", res.data);
                // start:保存学科专业map到缓存
                let majorMap = {};
                res.data.forEach((item) => {
                    majorMap["eduMajor_" + item.code] = item;
                });
                storage.set("eduMajorMap", majorMap);
                // end:保存学科专业map到缓存
                resolve();
            },
            stateFail: (res) => {
                uni.showToast({
                    title: "读取学科专业列表错误：" + res.errMsg,
                    icon: "none"
                })
                reject();
            }
        })
    });
}

/**
 * 获取所有门类及子专业
 * /tool-edu/major/getAllHasChildren
 */
let requestEduMajorListHasChildren = function() {
    return new Promise((resolve, reject) => {
        requestSecurity.send({
            uri: "/tool-edu/major/getAllHasChildren",
            stateSuccess: (res) => {
                console.debug("获取学科专业列表成功", res.data);
                // 保存学科专业列表到缓存
                storage.set("eduMajorListHasChildren", res.data);
                let majorL2Map = {};
                res.data.forEach((item) => {
                    item.children.forEach((child1) => {
                        child1.children.forEach((child2) => {
                            majorL2Map["eduMajorL2_" + child2.code] = child2;
                        });
                    });
                });
                storage.set("eduMajorL2Map", majorL2Map);
                // end:保存学科专业map到缓存
                resolve();
            },
            stateFail: (res) => {
                uni.showToast({
                    title: "读取学科专业列表错误：" + res.errMsg,
                    icon: "none"
                })
                reject();
            }
        })
    });
}


/**
 * 获取所有职称等级
 */
let requestTitleLevelList = function() {
    return new Promise((resolve, reject) => {
        requestSecurity.send({
            uri: "/business-talent-web/titleLevel/getAll",
            stateSuccess: (res) => {
                console.debug("获取职称等级列表成功", res.data);
                // 保存职称等级列表到缓存
                storage.set("titleLevelList", res.data);
                // start:保存职称等级map到缓存
                let titleLevelMap = {};
                res.data.forEach((item) => {
                    titleLevelMap["titleLevel_" + item.id] = item;
                });
                storage.set("titleLevelMap", titleLevelMap);
                // end:保存职称等级map到缓存
                resolve();
            },
            stateFail: (res) => {
                uni.showToast({
                    title: "读取职称等级列表错误：" + res.errMsg,
                    icon: "none"
                })
                reject();
            }
        })
    });
}

/**
 * 获取所有工作专家人才等级
 */
let requestExpertLevelList = function() {
    return new Promise((resolve, reject) => {
        requestSecurity.send({
            uri: "/business-talent-web/expertLevel/getAll",
            stateSuccess: (res) => {
                console.debug("获取工作专家人才等级列表成功", res.data);
                // 保存工作专家人才等级列表到缓存
                storage.set("expertLevelList", res.data);
                // start:保存工作专家人才等级map到缓存
                let expertLevelMap = {};
                res.data.forEach((item) => {
                    expertLevelMap["expertLevel_" + item.id] = item;
                });
                storage.set("expertLevelMap", expertLevelMap);
                // end:保存工作专家人才等级map到缓存
                resolve();
            },
            stateFail: (res) => {
                uni.showToast({
                    title: "读取工作专家人才等级列表错误：" + res.errMsg,
                    icon: "none"
                })
                reject();
            }
        })
    });
}


export default {
    requestEduMajorListHasChildren,
    requestWorkSeriesList,
    requestSkillLevelList,
    requestExpertLevelList,
    requestTalentPlanList,
    requestTalentAwardList,
    requestEduMajorList,
    requestTitleLevelList
}